package com.taiji.workOrder.Utils;



import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: demo
 * @description:
 * @author: Wang.zw
 * @create: 2020-05-09 16:25
 **/
public class ChineseTextToPinyin {

    /***
     * 将汉字转成拼音(取首字母或全拼)
     *
     * @param hanzi
     * @param flag  是否全拼 false是不全拼
     * @return
     */
    public static String convertHanzi2Pinyin(String hanzi, boolean flag) {
        /***
         * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
         * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
         * ^[\u4E00-\u9FA5]+$ 匹配简体
         */
        String regExp = "^[\u4E00-\u9FFF]+$";
        StringBuilder sb = new StringBuilder();
        if (hanzi == null || "".equals(hanzi.trim())) {
            return "";
        }
        String pinyin = "";
        for (int i = 0; i < hanzi.length(); i++) {
            char unit = hanzi.charAt(i);
            if (match(String.valueOf(unit), regExp)) { //是汉字，则转拼音
                pinyin = convertSingleHanzi2Pinyin(unit);
                if (flag) {
                    sb.append(pinyin);
                } else {
                    sb.append(pinyin.charAt(0));
                }
            } else {
                sb.append(unit);
            }
        }
        return sb.toString().toUpperCase();
    }

    /***
     * 将单个汉字转成拼音
     *
     * @param hanzi
     * @return
     */
    private static String convertSingleHanzi2Pinyin(char hanzi) {
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] res;
        StringBuilder sb = new StringBuilder();
        try {
            res = PinyinHelper.toHanyuPinyinStringArray(hanzi, outputFormat);
            sb.append(res[0]);//对于多音字，只用第一个拼音
        } catch (Exception e) {
            return "";
        }
        return sb.toString();
    }

    /***
     * @param str   源字符串
     * @param regex 正则表达式
     * @return 是否匹配
     */
    public static boolean match(String str, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }


    public static void main(String[] args) {
        System.out.println(ChineseTextToPinyin.convertHanzi2Pinyin("专项资金管理", false));
    }


    public static String ramdomNumber() {
        // TODO Auto-generated method stub
        String str = "ABCDEFGHIJKLMNOBQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(36));
            sb.append(ch);
        }
        return sb.toString();
    }
}